package org.sbbs.base.dao.jdbc;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.sbbs.base.dao.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.orm.hibernate4.SessionFactoryUtils;

/**
 * @author Administrator
 * @param <T>
 * @param <PK>
 */
public class BaseDaoJdbc<T, PK extends Serializable> implements BaseDao<T, PK> {
	/**
	 * 
	 */
	protected final Log log = LogFactory.getLog(getClass());
	/**
	 * 
	 */
	private Class<T> persistentClass;
	private String persistentTable;
	private String primaryKey;

	/**
	 * 
	 */
	private JdbcTemplate jdbcTemplate;
	/**
	 * 搜索
	 */
	private SessionFactory sessionFactory;

	/**
	 * @param persistentClass
	 */
	public BaseDaoJdbc(final Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

	/**
	 * @param persistentClass
	 * @param sessionFactory
	 */
	public BaseDaoJdbc(final Class<T> persistentClass,
			final SessionFactory sessionFactory) {
		this.persistentClass = persistentClass;

		Configuration conf = new Configuration();
		persistentTable = conf.getClassMapping(persistentTable).getTable()
				.getName();
		primaryKey = conf.getClassMapping(persistentTable).getTable()
				.getPrimaryKey().getName();
		this.sessionFactory = sessionFactory;
		this.jdbcTemplate = new JdbcTemplate(SessionFactoryUtils
				.getDataSource(sessionFactory));
	}

	/**
	 * @return
	 */
	public final Class<T> getPersistentClass() {
		return persistentClass;
	}

	/**
	 * @param persistentClass
	 */
	public final void setPersistentClass(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

	/**
	 * @return
	 */
	public final SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory
	 */
	@Autowired
	@Required
	public final void setSessionFactory(final SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
		this.jdbcTemplate = new JdbcTemplate(SessionFactoryUtils
				.getDataSource(sessionFactory));
	}

	/**
	 * @return
	 */
	public final JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	// ParameterizedBeanPropertyRowMapper
	@Override
	public final List<T> getAll() {
		// TODO Auto-generated method stub
		return this.jdbcTemplate
				.query("SELECT * FROM " + this.persistentTable,
						ParameterizedBeanPropertyRowMapper
								.newInstance(persistentClass));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sbbs.base.dao.BaseDao#getAllDistinct()
	 */
	@Override
	public final List<T> getAllDistinct() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sbbs.base.dao.BaseDao#get(java.io.Serializable)
	 */
	@Override
	public final T get(final PK id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public final boolean exists(final PK id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public final T save(final T object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void remove(final PK id) {
		// TODO Auto-generated method stub

	}
}
